<template>
 <div v-if="item.isRange">
    <el-date-picker
      style="width:152px;"
      @change="changeStartTime"
      v-model="item.startTime"
      format="yyyy-MM-dd"
      :clearable="false"
      :editable="false"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
    <el-date-picker
      style="width:152px;"
      @change="changeEndTime"
      v-model="item.endTime"
      format="yyyy-MM-dd"
      :clearable="false"
      :editable="false"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
 </div>
 <div v-else>
    <el-date-picker
      style="width:152px;"
      @change="changeStartTime"
      v-model="item.startTime"
      format="yyyy-MM-dd"
      :clearable="false"
      :editable="false"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
 </div>
</template>
<script>
export default {
  data() {
    return {
      item: {
        startTime: '',
        endTime: '',
        isRange: false
      }
    };
  },
  methods: {
    changeStartTime(val) {
      this.item.startTime = val + ' 00:00:00';
      this.$emit('change', this.item);
    },
    changeEndTime(val) {
      this.item.endTime = val + ' 23:59:59';
      this.$emit('change', this.item);
    }
  },
  created() {
    this.item = Object.assign(this.item, JSON.parse(JSON.stringify(this.$attrs)));
    // this.item.startTime = this.item.startTime || this.$moment().format('YYYY-MM-DD') + ' 00:00:00';
    // this.item.endTime = this.item.endTime || this.$moment().format('YYYY-MM-DD') + ' 23:59:59';
    // this.$emit('change', this.item);
    if (this.item.isRange) {
      if (this.item.startTime) {
        this.item.startTime = this.$moment(this.item.startTime).startOf('days').format('YYYY-MM-DD') + ' 00:00:00';
      } else {
        this.item.startTime = this.$moment().format('YYYY-MM-DD') + ' 00:00:00';
      }
      if (this.item.endTime) {
        this.item.endTime = this.$moment(this.item.endTime).endOf('days').format('YYYY-MM-DD') + ' 23:59:59';
      } else {
        this.item.endTime = this.$moment().endOf('days').format('YYYY-MM-DD') + ' 23:59:59';
      }
    } else {
      if (this.item.startTime) {
        this.item.startTime = this.$moment(this.item.startTime).startOf('days').format('YYYY-MM-DD') + ' 00:00:00';
        this.item.endTime = this.$moment(this.item.startTime).endOf('days').format('YYYY-MM-DD') + ' 23:59:59';
      } else {
        this.item.startTime = this.$moment().startOf('days').format('YYYY-MM-DD') + ' 00:00:00';
        this.item.endTime = this.$moment().endOf('days').format('YYYY-MM-DD') + ' 23:59:59';
      }
    }
    this.$emit('change', this.item);
  }
};
</script>